package cn.com.xf.web.controller;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.com.xf.common.AbstractBaseController;
import cn.com.xf.common.Constants;
import cn.com.xf.common.StringUtil;
import cn.com.xf.domain.TblCheckLeave;
import cn.com.xf.domain.TblCheckMend;
import cn.com.xf.domain.TblCheckMend;
import cn.com.xf.domain.TblCheckMend;
import cn.com.xf.domain.TblCheckMend;
import cn.com.xf.domain.TblCheckMend;
import cn.com.xf.service.CompanyService;
import cn.com.xf.service.MendService;
import cn.com.xf.utils.DateUtils;
import cn.com.xf.utils.encode.EncrypTool;
import cn.com.xf.web.PageModel;
import cn.com.xf.web.form.LeaveForm;
import cn.com.xf.web.form.MendForm;
import cn.com.xf.web.form.MendForm;
import cn.com.xf.web.form.MendForm;
import cn.com.xf.web.form.MendForm;
import cn.com.xf.web.form.MendForm;
import cn.com.xf.web.security.UserInfo;

/**
 * 补卡管理
 *
 */
@Controller
@RequestMapping("/check")
public class MendController extends AbstractBaseController {
	
	@Autowired
	private MendService mendService;
	@Autowired
	private CompanyService companyService;
	
	@RequestMapping("/mendList")
	public ModelAndView mendList(HttpServletRequest request, HttpServletResponse response, MendForm form)
			throws Exception {
		ModelAndView mv = null;
		String year_month = StringUtil.StringFilter(form.getYear_month());
		String siteid = StringUtil.StringFilter(form.getSiteid());
		String userid = StringUtil.StringFilter(form.getUserid());
		String deptid = StringUtil.StringFilter(form.getDeptid());
		if(siteid.equals("") && deptid.equals("")) {
			mv = new ModelAndView("/check/mend_list", Constants.FORM_COMMAND_KEY, form);		
		}else {
			mv = new ModelAndView("/check/mend_dept_list", Constants.FORM_COMMAND_KEY, form);
		}
		form.setYear_month(year_month);
		form.setSiteid(siteid);
		form.setUserid(userid);
		form.setDeptid(deptid!=null && deptid.equals("D999")?"":deptid);
		int count = mendService.selectMendCount(year_month,userid, siteid, deptid!=null && deptid.equals("D999")?"":deptid);
		if (count == 0)
			return mv;

		int pageNum = 1;
		if (StringUtils.isNotBlank(form.getPageNum())) {
			pageNum = Integer.valueOf(form.getPageNum());
		}
		PageModel pageModel = new PageModel(count, PageModel.DEFAULT_PAGE_SIZE, pageNum, form);
		int begin = pageModel.getBegin();
		int pageSize = pageModel.getPageSize();
		List<TblCheckMend> list = mendService.selectMend(begin, pageSize,year_month, userid, siteid, deptid!=null && deptid.equals("D999")?"":deptid);
		mv.addObject("list", list);
		return mv;
	}
	
	@RequestMapping("/addMend")
	public ModelAndView addMend(HttpServletRequest request, HttpServletResponse response, MendForm form)
			throws Exception {
		ModelAndView mv = new ModelAndView("/check/add_mend", Constants.FORM_COMMAND_KEY, form);		
		return mv;
	}
	@RequestMapping("/doAddMend")
	public ModelAndView doAddMend(HttpServletRequest request, HttpServletResponse response, @Valid MendForm form,
			BindingResult result) throws Exception {
		ModelAndView mv = new ModelAndView("/check/add_result", Constants.FORM_COMMAND_KEY, form);
		List<ObjectError> errorList = null;
		if (result.hasErrors()) {
			errorList = result.getAllErrors();
			String errorJson = errorToJson(errorList);
			mv.addObject("errorJson", errorJson);
			return mv;
		}
		if(form.getCheckdate().equals("")) {
			FieldError e = new FieldError("", "checkdate", "补卡日期不能为空");
			errorList = new ArrayList<ObjectError>();
			errorList.add(e);
			String errorJson = errorToJson(errorList);
			mv.addObject("errorJson", errorJson);
			return mv;
		}
		String time1 = form.getTime1()!=null?form.getTime1():"";
		String time2 = form.getTime2()!=null?form.getTime2():"";
		String time3 = form.getTime3()!=null?form.getTime3():"";
		if(time1.equals("") && time2.equals("") && time3.equals("")) {
			FieldError e = new FieldError("", "time1", "补卡时段必填一项");
			errorList = new ArrayList<ObjectError>();
			errorList.add(e);
			String errorJson = errorToJson(errorList);
			mv.addObject("errorJson", errorJson);
			return mv;
		}
		if(form.getReason() != null && form.getReason().equals("")) {
			FieldError e = new FieldError("", "reason", "补卡原因不能为空");
			errorList = new ArrayList<ObjectError>();
			errorList.add(e);
			String errorJson = errorToJson(errorList);
			mv.addObject("errorJson", errorJson);
			return mv;
		}
		
		String checkdate = form.getCheckdate();
		TblCheckMend mend = new TblCheckMend();
		mend.setUserid(form.getUserid());
		mend.setSiteid(form.getSiteid());
		mend.setCheckdate(DateUtils.parseDate(checkdate));
		mend.setType(new Short(form.getType()));
		if (!time1.equals("")) {
			String datetime1 = checkdate + " " + time1;
			mend.setTime1(DateUtils.parseDatetime(datetime1));
		} else {
			mend.setTime1(null);
		}
		if (!time2.equals("")) {
			String datetime2 = checkdate + " " + time2;
			mend.setTime2(DateUtils.parseDatetime(datetime2));
		} else {
			mend.setTime2(null);
		}
		if (!time3.equals("")) {
			String datetime3 = checkdate + " " + time3;
			mend.setTime3(DateUtils.parseDatetime(datetime3));
		} else {
			mend.setTime3(null);
		}
		mend.setReason(form.getReason());
		mend.setAddtime(new Date());
		mend.setFlag(0);
		mend.setDeptflag(0);
		mend.setPersonflag(4);
		mendService.insertMend(mend);
		mv.addObject("status", "SUCCESS");
		return mv;
	}
	
	@RequestMapping("/editMend")
	public ModelAndView editMend(HttpServletRequest request, HttpServletResponse response, MendForm form)
			throws Exception {
		ModelAndView mv = new ModelAndView("/check/edit_mend", Constants.FORM_COMMAND_KEY, form);
		Long id = new Long(form.getId());
		TblCheckMend mend = mendService.selectMendById(id);
		mv.addObject("mend", mend);
		return mv;
	}
	
	@RequestMapping("/doEditMend")
	public ModelAndView doEditMend(HttpServletRequest request, HttpServletResponse response, @Valid MendForm form,
			BindingResult result) throws Exception {
		ModelAndView mv = new ModelAndView("/check/add_result", Constants.FORM_COMMAND_KEY, form);
		List<ObjectError> errorList = null;
		if (result.hasErrors()) {
			errorList = result.getAllErrors();
			String errorJson = errorToJson(errorList);
			mv.addObject("errorJson", errorJson);
			return mv;
		}
		if(form.getCheckdate().equals("")) {
			FieldError e = new FieldError("", "checkdate", "补卡日期不能为空");
			errorList = new ArrayList<ObjectError>();
			errorList.add(e);
			String errorJson = errorToJson(errorList);
			mv.addObject("errorJson", errorJson);
			return mv;
		}
		String time1 = form.getTime1()!=null?form.getTime1():"";
		String time2 = form.getTime2()!=null?form.getTime2():"";
		String time3 = form.getTime3()!=null?form.getTime3():"";
		if(time1.equals("") && time2.equals("") && time3.equals("")) {
			FieldError e = new FieldError("", "time1", "补卡时段必填一项");
			errorList = new ArrayList<ObjectError>();
			errorList.add(e);
			String errorJson = errorToJson(errorList);
			mv.addObject("errorJson", errorJson);
			return mv;
		}		
		if(form.getReason() != null && form.getReason().equals("")) {
			FieldError e = new FieldError("", "reason", "补卡原因不能为空");
			errorList = new ArrayList<ObjectError>();
			errorList.add(e);
			String errorJson = errorToJson(errorList);
			mv.addObject("errorJson", errorJson);
			return mv;
		}		
		String checkdate = form.getCheckdate();
		TblCheckMend mend = new TblCheckMend();
		mend.setId(new Long(form.getId()));
		mend.setUserid(form.getUserid());
		mend.setSiteid(form.getSiteid());
		mend.setCheckdate(DateUtils.parseDate(checkdate));
		mend.setType(new Short(form.getType()));
		if (!time1.equals("")) {
			String datetime1 = checkdate + " " + time1;
			mend.setTime1(DateUtils.parseDatetime(datetime1));
		} else {
			mend.setTime1(null);
		}
		if (!time2.equals("")) {
			String datetime2 = checkdate + " " + time2;
			mend.setTime2(DateUtils.parseDatetime(datetime2));
		} else {
			mend.setTime2(null);
		}
		if (!time3.equals("")) {
			String datetime3 = checkdate + " " + time3;
			mend.setTime3(DateUtils.parseDatetime(datetime3));
		} else {
			mend.setTime3(null);
		}
		mend.setReason(form.getReason());
		mend.setAddtime(new Date());
		mend.setDeptflag(0);
		mendService.updateToEdit(mend);
		mv.addObject("status", "SUCCESS");
		return mv;		
	}
	
	@RequestMapping("/deleteMend")
	@ResponseBody
	public Map<String, Object> deleteMend(HttpServletRequest request, HttpServletResponse response, MendForm form)
			throws Exception {		
		int result = mendService.deleteMend(new Long(form.getId()));
		Map<String, Object> map = new HashMap<String, Object>();
		if (result < 1) {
			map.put("status", "FAILD");
		} else {
			map.put("status", "SUCCESS");
		}
		return map;
	}
	
	@RequestMapping("/auditDeptMend")
	@ResponseBody
	public Map<String, Object> auditDeptMend(HttpServletRequest request, HttpServletResponse response, MendForm form)
			throws Exception {		
		Map<String, Object> map = new HashMap<String, Object>();
		TblCheckMend mend = new TblCheckMend();		
		mend.setId(new Long(form.getId()));
		mend.setDeptflag(2);
		mend.setDeptuser(form.getDeptuser());
		mendService.updateUserMend(mend);
		map.put("status", "SUCCESS");		
		return map;
	}
	
	@RequestMapping("/auditPersonMend")
	@ResponseBody
	public Map<String, Object> auditPersonMend(HttpServletRequest request, HttpServletResponse response, MendForm form)
			throws Exception {		
		Map<String, Object> map = new HashMap<String, Object>();
		String personuser = ((UserInfo)SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getName();
		TblCheckMend mend = new TblCheckMend();		
		mend.setId(new Long(form.getId()));
		mend.setPersonflag(Integer.valueOf(form.getPersonflag()));
		mend.setPersonuser(personuser);
		mendService.updateUserMend(mend);
		map.put("status", "SUCCESS");		
		return map;
	}
	
	@RequestMapping("/deptDestroyMend")
	@ResponseBody
	public Map<String, Object> deptDestroyMend(HttpServletRequest request, HttpServletResponse response, MendForm form)
			throws Exception {		
		Map<String, Object> map = new HashMap<String, Object>();
		TblCheckMend mend = new TblCheckMend();		
		mend.setId(new Long(form.getId()));
		mend.setDeptflag(3);
		mend.setDeptuser(form.getDeptuser());
		mendService.updateUserMend(mend);
		map.put("status", "SUCCESS");		
		return map;
	}
	@RequestMapping("/personDestroyMend")
	@ResponseBody
	public Map<String, Object> personDestroyMend(HttpServletRequest request, HttpServletResponse response, MendForm form)
			throws Exception {		
		Map<String, Object> map = new HashMap<String, Object>();
		TblCheckMend leave = new TblCheckMend();	
		String personuser = ((UserInfo)SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getName();
		leave.setId(new Long(form.getId()));
		leave.setPersonflag(3);
		leave.setPersonuser(personuser);
		mendService.updateUserMend(leave);
		map.put("status", "SUCCESS");		
		return map;
	}
	
	@RequestMapping("/noDeptMend")
	public ModelAndView noDeptMend(HttpServletRequest request, HttpServletResponse response, MendForm form)
			throws Exception {
		ModelAndView mv = new ModelAndView("/check/edit_mend", Constants.FORM_COMMAND_KEY, form);
		Long id = new Long(form.getId());
		TblCheckMend mend = mendService.selectMendById(id);
		mv.addObject("mend", mend);
		mv.addObject("destroy",2);
		return mv;
	}
	
	@RequestMapping("/noPersonMend")
	public ModelAndView noPersonMend(HttpServletRequest request, HttpServletResponse response, MendForm form)
			throws Exception {
		ModelAndView mv = new ModelAndView("/check/edit_mend", Constants.FORM_COMMAND_KEY, form);
		Long id = new Long(form.getId());
		TblCheckMend mend = mendService.selectMendById(id);
		mv.addObject("mend", mend);
		mv.addObject("destroy",3);
		return mv;
	}
	
	@RequestMapping("/doNoDeptMend")
	public ModelAndView doNoDeptMend(HttpServletRequest request, HttpServletResponse response, @Valid MendForm form,
			BindingResult result) throws Exception {
		ModelAndView mv = new ModelAndView("/check/add_result", Constants.FORM_COMMAND_KEY, form);
		List<ObjectError> errorList = null;
		if (result.hasErrors()) {
			errorList = result.getAllErrors();
			String errorJson = errorToJson(errorList);
			mv.addObject("errorJson", errorJson);
			return mv;
		}
		
		if(form.getDeptremark() != null && form.getDeptremark().equals("")) {
			FieldError e = new FieldError("", "deptremark", "不通过原因不能为空");
			errorList = new ArrayList<ObjectError>();
			errorList.add(e);
			String errorJson = errorToJson(errorList);
			mv.addObject("errorJson", errorJson);
			return mv;
		}		
		TblCheckMend mend = new TblCheckMend();		
		mend.setId(new Long(form.getId()));		
		mend.setDeptflag(new Integer(form.getDeptflag()));
		mend.setDeptremark(form.getDeptremark());
		mend.setDeptuser(form.getDeptuser());
		mendService.updateUserMend(mend);
		mv.addObject("status", "SUCCESS");
		return mv;		
	}
	
	@RequestMapping("/doNoPersonMend")
	public ModelAndView doNoPersonMend(HttpServletRequest request, HttpServletResponse response, @Valid MendForm form,
			BindingResult result) throws Exception {
		ModelAndView mv = new ModelAndView("/check/add_result", Constants.FORM_COMMAND_KEY, form);
		List<ObjectError> errorList = null;
		if (result.hasErrors()) {
			errorList = result.getAllErrors();
			String errorJson = errorToJson(errorList);
			mv.addObject("errorJson", errorJson);
			return mv;
		}
		
		if(form.getPersonremark() != null && form.getPersonremark().equals("")) {
			FieldError e = new FieldError("", "deptremark", "不通过原因不能为空");
			errorList = new ArrayList<ObjectError>();
			errorList.add(e);
			String errorJson = errorToJson(errorList);
			mv.addObject("errorJson", errorJson);
			return mv;
		}		
		TblCheckMend mend = new TblCheckMend();		
		mend.setId(new Long(form.getId()));		
		mend.setPersonflag(new Integer(form.getPersonflag()));
		mend.setPersonremark(form.getPersonremark());
		mend.setPersonuser(form.getPersonuser());
		mendService.updateUserMend(mend);
		mv.addObject("status", "SUCCESS");
		return mv;		
	}
	
	@RequestMapping("/userDestroyMend")
	public ModelAndView userDestroyMend(HttpServletRequest request, HttpServletResponse response, MendForm form)
			throws Exception {
		ModelAndView mv = new ModelAndView("/check/edit_mend", Constants.FORM_COMMAND_KEY, form);
		Long id = new Long(form.getId());
		TblCheckMend mend = mendService.selectMendById(id);
		mv.addObject("mend", mend);
		mv.addObject("destroy",1);
		return mv;
	}
	
	@RequestMapping("/userDelMend")
	@ResponseBody
	public Map<String, Object> userDelMend(HttpServletRequest request, HttpServletResponse response, MendForm form)
			throws Exception {		
		Map<String, Object> map = new HashMap<String, Object>();
		TblCheckMend mend = new TblCheckMend();		
		mend.setId(new Long(form.getId()));
		mend.setFlag(1);
		mendService.updateUserMend(mend);
		map.put("status", "SUCCESS");		
		return map;
	}
	
	@RequestMapping("/deptDelMend")
	@ResponseBody
	public Map<String, Object> deptDelMend(HttpServletRequest request, HttpServletResponse response, MendForm form)
			throws Exception {		
		Map<String, Object> map = new HashMap<String, Object>();
		TblCheckMend mend = new TblCheckMend();		
		mend.setId(new Long(form.getId()));
		mend.setDeptflag(0);
		mendService.updateUserMend(mend);
		map.put("status", "SUCCESS");		
		return map;
	}
	
	@RequestMapping("/doUserDestroyMend")
	public ModelAndView doUserDestroyMend(HttpServletRequest request, HttpServletResponse response, @Valid MendForm form,
			BindingResult result) throws Exception {
		ModelAndView mv = new ModelAndView("/check/add_result", Constants.FORM_COMMAND_KEY, form);
		List<ObjectError> errorList = null;
		if (result.hasErrors()) {
			errorList = result.getAllErrors();
			String errorJson = errorToJson(errorList);
			mv.addObject("errorJson", errorJson);
			return mv;
		}
		
		if(form.getRemark() != null && form.getRemark().equals("")) {
			FieldError e = new FieldError("", "remark", "销毁原因不能为空");
			errorList = new ArrayList<ObjectError>();
			errorList.add(e);
			String errorJson = errorToJson(errorList);
			mv.addObject("errorJson", errorJson);
			return mv;
		}		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		TblCheckMend mend = new TblCheckMend();
		mend.setId(new Long(form.getId()));		
		mend.setAddtime(new Date());
		mend.setFlag(new Integer(form.getFlag()));
		mend.setRemark(form.getRemark());
		mendService.updateUserMend(mend);
		mv.addObject("status", "SUCCESS");
		return mv;		
	}
	
	
	@RequestMapping("/mendPersonList")
	public ModelAndView mendPersonList(HttpServletRequest request, HttpServletResponse response, MendForm form)
			throws Exception {
		ModelAndView mv = new ModelAndView("/check/mend_person_list", Constants.FORM_COMMAND_KEY, form);
		String year_month = StringUtil.StringFilter(form.getYear_month());
		String siteid = StringUtil.StringFilter(form.getSiteid());
		String userid = StringUtil.StringFilter(form.getUserid());
		String deptid = StringUtil.StringFilter(form.getDeptid());
		if (siteid.equals("") || !isSiteId(siteid)) {
			siteid = getDefaultSiteid();
		}
		form.setYear_month(year_month);
		form.setSiteid(siteid);
		form.setUserid(userid);
		form.setDeptid(deptid!=null && deptid.equals("D999")?"":deptid);
		int count = mendService.selectPersonMendCount(year_month,userid, siteid, deptid!=null && deptid.equals("D999")?"":deptid);
		if (count == 0)
			return mv;

		int pageNum = 1;
		if (StringUtils.isNotBlank(form.getPageNum())) {
			pageNum = Integer.valueOf(form.getPageNum());
		}
		PageModel pageModel = new PageModel(count, 20, pageNum, form);
		int begin = pageModel.getBegin();
		int pageSize = pageModel.getPageSize();
		List<TblCheckMend> list = mendService.selectPersonMend(begin, pageSize,year_month, userid, siteid, deptid!=null && deptid.equals("D999")?"":deptid);
		Map<String, Object> map = companyService.getDetps();
		for (TblCheckMend mend: list) {
			if (mend.getDeptid() == null) {
				mend.setDeptname("");
			} else {
				mend.setDeptname(map.get(mend.getDeptid())+"");
			}
		}
		mv.addObject("list", list);
		return mv;
	}
	@RequestMapping("/addPersonMend")
	public ModelAndView addPersonMend(HttpServletRequest request, HttpServletResponse response, MendForm form)
			throws Exception {
		ModelAndView mv = new ModelAndView("/check/add_person_mend", Constants.FORM_COMMAND_KEY, form);
		mv.addObject("defaultSiteid", getDefaultSiteid());
		return mv;
	}
	
	@RequestMapping("/doAddPersonMend")
	@ResponseBody
	public Map<String, Object> doAddPersonMend(HttpServletRequest request, HttpServletResponse response, @Valid MendForm form,
			BindingResult result) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		List<ObjectError> errorList = null;
		if (result.hasErrors()) {
			errorList = result.getAllErrors();
			String errorJson = errorToJson(errorList);
			map.put("errorJson", errorJson);
			return map;
		}		
		String personuser = ((UserInfo)SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getName();
		String  mendJson = form.getMendJson();
		JSONParser jp = new JSONParser();
		JSONArray formJson = (JSONArray) jp.parse(mendJson);
		List<MendForm> mendList = new ArrayList<MendForm>();
		for (int i = 0; i < formJson.size(); i++) {
			JSONObject jons = (JSONObject)formJson.get(i);
			MendForm mf = new MendForm();
			mf.setAdduser(personuser);
			int rowIndex = i+1;
			if(!isSiteId(jons.get("siteid")+"")) {
				map.put("status", "FAILD");
				map.put("msg", "第&nbsp<font color='#FF0000'>"+rowIndex+"</font>&nbsp条记录所属公司不合法，请校对后单独添加！");
				return map;
			}
			if((jons.get("time1")+"").equals("") && (jons.get("time2")+"").equals("") && (jons.get("time3")+"").equals("")){
				map.put("status", "FAILD");
				map.put("msg", "第&nbsp<font color='#FF0000'>"+rowIndex+"</font>&nbsp条记录至少要有一个时段不为空！");
				return map;
			}
			mf.setSiteid(jons.get("siteid")+"");
			mf.setCheckdate(jons.get("checkdate")+"");
			mf.setReason(jons.get("reason")+"");
			mf.setTime1(jons.get("time1")+"");
			mf.setTime2(jons.get("time2")+"");
			mf.setTime3(jons.get("time3")+"");
			mf.setUserid(jons.get("userid")+"");
			mf.setType(jons.get("type")+"");
			mf.setFlag(2);
			mf.setDeptflag(2);
			mf.setPersonflag(0);
			mendList.add(mf);			
		}
		for (MendForm mendForm : mendList) {
			if (!mendForm.getUserid().contains(",")) {
				TblCheckMend mend = mendForm.copyTo();
				mendService.insertMend(mend);
			} else {
				TblCheckMend mend = mendForm.copyTo();
				String[] userids = mendForm.getUserid().split(",");
				for (String userid: userids) {
					mend.setUserid(userid);
					mendService.insertMend(mend);
				}
			}
		}
		map.put("status", "SUCCESS");
		return map;
	}
	
	@RequestMapping("/exportMend")
	public void exportMend(HttpServletRequest request, HttpServletResponse response, MendForm form)
			throws Exception {
		String siteid = form.getSiteid();
		String deptid = StringUtil.StringFilter(form.getDeptid()!=null &&form.getDeptid().equals("D999")?"":form.getDeptid());
		String userid = StringUtil.StringFilter(form.getUserid());
		String sitename = StringUtil.StringFilter(form.getSitename());
		String year_Month = form.getYear_month();
		List<TblCheckMend> list = mendService.selectExport(year_Month,siteid, deptid, userid);
		String sheetName = year_Month.replaceAll("-", "年")+"月"+sitename+deptid+"补卡记录";
		String[] title= {"所属公司","部门","工号","姓名","类型","考勤日期","打卡时间","补卡原因","操作人","操作时间"};
		// 1.创建一个workbook，对应一个Excel文件
		HSSFWorkbook wb = new HSSFWorkbook();
	    // 2.在workbook中添加一个sheet，对应Excel中的一个sheet
	    HSSFSheet sheet = wb.createSheet(sheetName);
	    // 3.在sheet中添加表头第0行，老版本poi对excel行数列数有限制short
	    sheet.createFreezePane( 0, 1, 0, 1 );
	    HSSFRow row = sheet.createRow((int) 0);
	    // 4.创建单元格，设置值表头，设置表头居中
	    HSSFCellStyle style = wb.createCellStyle();
	    // 居中格式
	    style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
	    for (int i = 0; i < title.length; i++) {    
            HSSFCell cell = row.createCell(i);    
            cell.setCellValue(title[i]);    
            cell.setCellStyle(style);    
            sheet.setColumnWidth(i, 6*2*256);
        }
	    for (int i = 0; i < list.size(); i++) {    
            row = sheet.createRow(i + 1);    
            TblCheckMend mend = list.get(i); 
            row.createCell(0).setCellValue(mend.getSitename());
            row.createCell(1).setCellValue(mend.getDeptname()!=null?mend.getDeptname():"");
            row.createCell(2).setCellValue(mend.getUserid());    
            row.createCell(3).setCellValue(mend.getUsername());    
            row.createCell(4).setCellValue(mend.getTypename());
            row.createCell(5).setCellValue(DateFormatUtils.format(mend.getCheckdate(), "yyyy-MM-dd"));
            row.createCell(6).setCellValue(mend.getChecktime());
            row.createCell(7).setCellValue(mend.getReason());
            row.createCell(8).setCellValue(mend.getOperateUser());
            row.createCell(9).setCellValue(DateUtils.formatDatetime(mend.getOperateTime()));
        }
	    response.setContentType("application/vnd.ms-excel;charset=utf-8");
	    response.setHeader("Content-Disposition", "attachment;filename="
	            + new String((sheetName + ".xls").getBytes(), "iso-8859-1"));
	    OutputStream ouputStream = response.getOutputStream();    
        wb.write(ouputStream);    
        ouputStream.flush();    
        ouputStream.close();
	}
	
	
	
	

	@RequestMapping("/editPersonMend")
	public ModelAndView editPersonMend(HttpServletRequest request, HttpServletResponse response, MendForm form)
			throws Exception {
		ModelAndView mv = new ModelAndView("/check/edit_mend", Constants.FORM_COMMAND_KEY, form);
		Long id = new Long(form.getId());
		TblCheckMend mend = mendService.selectMendById(id);
		mv.addObject("mend", mend);
		mv.addObject("destroy",4);
		return mv;
	}
	
	
	@RequestMapping("/doEditPersonMend")
	public ModelAndView doEditPersonMend(HttpServletRequest request, HttpServletResponse response, @Valid MendForm form,
			BindingResult result) throws Exception {
		ModelAndView mv = new ModelAndView("/check/add_result", Constants.FORM_COMMAND_KEY, form);
		List<ObjectError> errorList = null;
		if (result.hasErrors()) {
			errorList = result.getAllErrors();
			String errorJson = errorToJson(errorList);
			mv.addObject("errorJson", errorJson);
			return mv;
		}
		if(form.getCheckdate().equals("")) {
			FieldError e = new FieldError("", "checkdate", "补卡日期不能为空");
			errorList = new ArrayList<ObjectError>();
			errorList.add(e);
			String errorJson = errorToJson(errorList);
			mv.addObject("errorJson", errorJson);
			return mv;
		}
		String time1 = form.getTime1()!=null?form.getTime1():"";
		String time2 = form.getTime2()!=null?form.getTime2():"";
		String time3 = form.getTime3()!=null?form.getTime3():"";
		if(time1.equals("") && time2.equals("") && time3.equals("")) {
			FieldError e = new FieldError("", "time1", "补卡时段必填一项");
			errorList = new ArrayList<ObjectError>();
			errorList.add(e);
			String errorJson = errorToJson(errorList);
			mv.addObject("errorJson", errorJson);
			return mv;
		}		
		if(form.getReason() != null && form.getReason().equals("")) {
			FieldError e = new FieldError("", "reason", "补卡原因不能为空");
			errorList = new ArrayList<ObjectError>();
			errorList.add(e);
			String errorJson = errorToJson(errorList);
			mv.addObject("errorJson", errorJson);
			return mv;
		}
		String personuser = ((UserInfo)SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getName();
		String checkdate = form.getCheckdate();
		TblCheckMend mend = new TblCheckMend();
		mend.setId(new Long(form.getId()));
		mend.setUserid(form.getUserid());
		mend.setSiteid(form.getSiteid());
		mend.setCheckdate(DateUtils.parseDate(checkdate));
		mend.setType(new Short(form.getType()));
		if (!time1.equals("")) {
			String datetime1 = checkdate + " " + time1;
			mend.setTime1(DateUtils.parseDatetime(datetime1));
		} else {
			mend.setTime1(null);
		}
		if (!time2.equals("")) {
			String datetime2 = checkdate + " " + time2;
			mend.setTime2(DateUtils.parseDatetime(datetime2));
		} else {
			mend.setTime2(null);
		}
		if (!time3.equals("")) {
			String datetime3 = checkdate + " " + time3;
			mend.setTime3(DateUtils.parseDatetime(datetime3));
		} else {
			mend.setTime3(null);
		}
		mend.setReason(form.getReason());
		mend.setAddtime(new Date());
		mend.setPersonuser(personuser);
		mendService.updateToEdit(mend);
		mv.addObject("status", "SUCCESS");
		return mv;	
	}
}
